package com.fm.service.impl;

import com.fm.common.ResultResponse;
import com.fm.dao.ProductMapper;
import com.fm.pojo.Product;
import com.fm.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("productService")
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    public ResultResponse saveOrUpdateProduct(Product product){
        if(product != null)
        {
            if(StringUtils.isNotBlank(product.getImage())){
                String[] subImageArray = product.getImage().split(",");
                if(subImageArray.length > 0){
                    product.setImage(subImageArray[0]);
                }
            }
            if(product.getId() != null){
                int rowCount = productMapper.updateByPrimaryKey(product);
                if(rowCount > 0){
                    return ResultResponse.isSuccessMessage("更新产品成功");
                }
                return ResultResponse.isSuccessMessage("更新产品失败");
            }else{
                int rowCount = productMapper.insert(product);
                if(rowCount > 0){
                    return ResultResponse.isSuccessMessage("新增产品成功");
                }
                return ResultResponse.isSuccessMessage("新增产品失败");
            }
        }
        return ResultResponse.isErrorMessage("新增或更新产品参数不正确");
    }

    public ResultResponse<String> delProduct(String productId){
        int resultCount = productMapper.deleteByPrimaryKey(productId);
        if(resultCount == 0){
            return ResultResponse.isErrorMessage("删除失败");
        }
        return ResultResponse.isSuccessMessage("删除成功");
    }

    public ResultResponse<String> refreshTime(String productId,int days){
        if(days>0){
            int rowCount = productMapper.updateTime(productId);
            if(rowCount > 0){
                return ResultResponse.isSuccessMessage("刷新成功");
            }
            return ResultResponse.isSuccessMessage("刷新失败");
        }
        return ResultResponse.isErrorMessage("获得商户权限或权限已过期");
    }

    public ResultResponse<String> updateStatus(String productId,int days){
        if(days>0){
            int rowCount = productMapper.updateStatus(productId);
            if(rowCount > 0){
                return ResultResponse.isSuccessMessage("发布状态已修改");
            }
            return ResultResponse.isSuccessMessage("发布状态修改失败");
        }
        return ResultResponse.isErrorMessage("获得商户权限或权限已过期");
    }

    public ResultResponse<PageInfo> getProductList(int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> orderList = productMapper.selectAllProduct();
        PageInfo pageResult = new PageInfo(orderList);
        pageResult.setList(orderList);
        return ResultResponse.isSuccess(pageResult);
    }

    public ResultResponse<PageInfo> getMyProductList(String userId,int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> orderList = productMapper.selectByUserId(userId);
        PageInfo pageResult = new PageInfo(orderList);
        pageResult.setList(orderList);
        return ResultResponse.isSuccess(pageResult);
    }

    public ResultResponse getProductDetail(String produceId){
        Product product=productMapper.selectByPrimaryKey(produceId);
        if(product!=null){
            return ResultResponse.isSuccess(product);
        }
        return ResultResponse.isErrorMessage("该产品不存在");
    }

}
